﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cairo;
using System.Windows.Forms;

namespace FigureDraw
{
    public class CairoAdaptee
    {
        Surface s;
        public void DrawLine(Panel panelDraw, Vector2 oldP, Vector2 curP, PenType penType)
        {
            System.Drawing.Graphics g = panelDraw.CreateGraphics();
            s = new Win32Surface(g.GetHdc());
            Context cr = new Context(s);
            cr.MoveTo(oldP.X, oldP.Y);
            cr.LineTo(curP.X, curP.Y);

            cr.LineWidth = 1;
            cr.Stroke();
        }

        internal void DrawLine(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            Panel panelDraw = new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }

            System.Drawing.Graphics g = panelDraw.CreateGraphics();
            s = new Win32Surface(g.GetHdc());
            Context cr = new Context(s);
            cr.MoveTo(oldP.X, oldP.Y);
            cr.LineTo(curP.X, curP.Y);

            cr.LineWidth = 1;
            cr.Stroke();
        }

        internal void DrawRectangle(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            Panel panelDraw = new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }
            System.Drawing.Graphics g = panelDraw.CreateGraphics();
            s = new Win32Surface(g.GetHdc());
            Context cr = new Context(s);
            
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            //Rectangle rect = new Rectangle();
            //rect.X = (p1.X > p2.X ? p2.X : p1.X);
            //rect.Y = (p1.Y > p2.Y ? p2.Y : p1.Y);
            //rect.Width = Math.Abs(p1.X - p2.X);
            //rect.Height = Math.Abs(p1.Y - p2.Y);

            cr.Rectangle((p1.X > p2.X ? p2.X : p1.X), (p1.Y > p2.Y ? p2.Y : p1.Y), Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
            cr.Stroke();
        }

        internal void DrawEclipse(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            Panel panelDraw = new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }
            System.Drawing.Graphics g = panelDraw.CreateGraphics();
            s = new Win32Surface(g.GetHdc());
            Context cr = new Context(s);

            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            
            int width = Math.Abs(p1.X - p2.X);
            int height=Math.Abs(p1.Y - p2.Y);
            int x = (p1.X > p2.X ? p2.X : p1.X) + width / 2;
            int y = (p1.Y > p2.Y ? p2.Y : p1.Y) + height/2;

            cr.Save();
            cr.Translate(x, y);
            cr.Scale(width / 2.0, height / 2.0);
            cr.Arc(0.0, 0.0, 1.0, 0.0, 2 * Math.PI);
            cr.Restore();
            cr.Stroke();

            //cr.MoveTo(p1.X, p1.Y + height / 500);
            //cr.CurveTo(p1.X, p1.Y, p1.X, p1.Y, p1.X + width / 500, p1.Y);
            //cr.CurveTo(p1.X + width, p1.Y, p1.X + width, p1.Y, p1.X + width, p1.Y + height / 500);
            //cr.CurveTo(p1.X + width, p1.Y + height, p1.X + width, p1.Y + height, p1.X + width / 500, p1.Y + height);
            //cr.CurveTo(p1.X, p1.Y + height, p1.X, p1.Y + height, p1.X, p1.Y + height / 500);

            
        }
        internal void SavePNG(string filePath)
        {
            s.WriteToPng(filePath);
        }

        internal void SaveJPG(string filePath)
        {
            s.WriteToPng(filePath);
        }
    }
}
